﻿using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.Web.Security;

namespace SPCIT.WebParts
{
    [Guid("ad5060b4-2b9a-4fb2-bf41-4b519c68cd47")]
    public class AddMembershipUser : System.Web.UI.WebControls.WebParts.WebPart
    {
        protected TextBox loginName;
        protected TextBox email;
        protected TextBox password;
        protected TextBox confirmPassword;
        protected CompareValidator comparePasswords;

        protected Button addCommand;
        protected Label result;

        protected override void CreateChildControls()
        {
            this.loginName = new TextBox();
            this.loginName.ID = "loginName";
            this.Controls.Add(this.loginName);

            this.email = new TextBox();
            this.email.ID = "email";
            this.Controls.Add(this.email);

            this.password = new TextBox();
            this.password.ID = "password";
            this.password.TextMode = TextBoxMode.Password;
            this.Controls.Add(this.password);

            this.confirmPassword = new TextBox();
            this.confirmPassword.ID = "confirmPassword";
            this.confirmPassword.TextMode = TextBoxMode.Password;
            this.Controls.Add(this.confirmPassword);

            this.comparePasswords = new CompareValidator();
            this.comparePasswords.ControlToValidate = this.password.ID;
            this.comparePasswords.ControlToCompare = this.confirmPassword.ID;
            this.comparePasswords.Operator = ValidationCompareOperator.Equal;
            this.comparePasswords.Type = ValidationDataType.String;
            this.comparePasswords.Text = "*";
            this.comparePasswords.Display = ValidatorDisplay.Dynamic;
            this.Controls.Add(this.comparePasswords);

            this.addCommand = new Button();
            this.addCommand.Click += new EventHandler(addCommand_Click);
            this.addCommand.Text = "Aggiungi utente";
            this.Controls.Add(this.addCommand);

            this.result = new Label();
            this.result.Text = String.Empty;
            this.Controls.Add(this.result);

            base.CreateChildControls();
        }

        void addCommand_Click(object sender, EventArgs e)
        {
            SPWeb webContext = SPControl.GetContextWeb(Context);
            if (webContext.CurrentUser.IsSiteAdmin)
            {
                if ((this.password.Text == this.confirmPassword.Text) && (!String.IsNullOrEmpty(this.loginName.Text)))
                {
                    try
                    {
                        if (Membership.Provider != null)
                        {
                            MembershipCreateStatus status;

                            Membership.Provider.CreateUser(
                                this.loginName.Text,
                                this.password.Text,
                                this.email.Text,
                                "Question", "Answer",
                                true, Guid.NewGuid(), out status);

                            if (status != MembershipCreateStatus.Success)
                            {
                                this.result.Text = "<BR>Creazione fallita. Stato: " + status.ToString();
                            }
                            else
                            {
                                this.result.Text = "<BR>Creazione riuscita. Stato: " + status.ToString();
                                this.loginName.Text = String.Empty;
                                this.password.Text = String.Empty;
                            }
                        }
                        else
                        {
                            this.result.Text = "<BR>Membership provider non configurato.";
                        }
                    }
                    catch (Exception ex)
                    {
                        this.result.Text = "<BR>Errore durante la creazione dell'utente.<br>" + ex.Message;
                    }
                }
                else
                {
                    result.Text = "<BR>La password e la conferma password non corrispondono, oppure il nome utente è vuoto!";
                }
            }
            else
            {
                result.Text = "<BR>Utente non abilitato ad eseguire l'operazione!";
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            SPWeb webContext = SPControl.GetContextWeb(Context);
            if (webContext.CurrentUser.IsSiteAdmin)
            {
                writer.Write("<table border=0>");

                writer.Write("<tr>");
                writer.Write("<td class='ms-vb'>");
                writer.Write("Nome utente:");
                writer.Write("</td>");
                writer.Write("<td class='ms-vb'>");
                this.loginName.RenderControl(writer);
                writer.Write("</td>");
                writer.Write("<td class='ms-vb'>");
                writer.Write("</td>");
                writer.Write("</tr>");

                writer.Write("<tr>");
                writer.Write("<td class='ms-vb'>");
                writer.Write("Email:");
                writer.Write("</td>");
                writer.Write("<td class='ms-vb'>");
                this.email.RenderControl(writer);
                writer.Write("</td>");
                writer.Write("<td class='ms-vb'>");
                writer.Write("</td>");
                writer.Write("</tr>");

                writer.Write("<tr>");
                writer.Write("<td class='ms-vb'>");
                writer.Write("Password:");
                writer.Write("</td>");
                writer.Write("<td class='ms-vb'>");
                this.password.RenderControl(writer);
                writer.Write("</td>");
                writer.Write("<td class='ms-vb'>");
                writer.Write("</td>");
                writer.Write("</tr>");

                writer.Write("<tr valign='top'>");
                writer.Write("<td class='ms-vb'>");
                writer.Write("Conferma password:");
                writer.Write("</td>");
                writer.Write("<td class='ms-vb'>");
                this.confirmPassword.RenderControl(writer);
                this.comparePasswords.RenderControl(writer); 
                writer.Write("</td>");
                writer.Write("<td class='ms-vb'>");
                writer.Write("</td>");
                writer.Write("</tr>");

                writer.Write("<tr valign='top'>");
                writer.Write("</td>");
                writer.Write("<td class='ms-vb'>");
                writer.Write("</td>");
                writer.Write("<td class='ms-vb'>");
                this.addCommand.RenderControl(writer);
                writer.Write("</td>");
                writer.Write("<td class='ms-vb'>");
                writer.Write("</td>");
                writer.Write("</tr>");
                
                writer.Write("</table>");
                this.result.RenderControl(writer);
            }
            else
            {
                this.result.Text += "<BR>Questa WebPart può essere utilizzata solo da utenti di livello SiteAdmin.";
                this.result.RenderControl(writer);
            }
        }
    }
}
